﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;

namespace Promote
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            gm = new GameManager(4);
            gm.Score.Score = Properties.Settings.Default.BestScore;

            Bind(gm);

            gm.Start();
        }

        private void Bind(GameManager gm)
        {
            BindingData data = gm.BindData;
            Binding bd;

            for (int i = 0; i < data.Size*data.Size; i++ )
            {
                Label l = tileGrid.FindName(String.Format("cell{0}", i + 1)) as Label;
                if(l==null)
                {
                    throw new PromoteException(String.Format("无法找到名称为 cell{0} 的元素", i + 1));
                }

                bd = new Binding();
                bd.Source = data.tilesData[i];
                bd.Path = new PropertyPath("Title");                   
                l.SetBinding(Label.ContentProperty, bd);

                bd = new Binding();
                bd.Source = data.tilesData[i];
                bd.Path = new PropertyPath("Value");
                bd.Converter = new StyleConvert();
                bd.ConverterParameter = this;
                l.SetBinding(Label.StyleProperty, bd);
            }

            bd = new Binding();
            bd.Source = gm.Score;
            bd.Path = new PropertyPath("Score");
            score.SetBinding(TextBlock.TextProperty, bd);

            bd = new Binding();
            bd.Source = gm.Score;
            bd.Path = new PropertyPath("BestScore");
            bestScore.SetBinding(TextBlock.TextProperty, bd);
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            switch(e.Key)
            {
                case Key.Up:    gm.Move(Direction.Up);      break;
                case Key.Down:  gm.Move(Direction.Down);    break;
                case Key.Left:  gm.Move(Direction.Left);    break;
                case Key.Right: gm.Move(Direction.Right);   break;
                case Key.F12:   gm.Start(); break;
            }
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            Properties.Settings.Default.BestScore = gm.Score.BestScore;
            Properties.Settings.Default.Save();
        }   

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {

        }

        private GameManager gm;
    }

    class StyleConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (targetType!=typeof(Style))
            {
                throw new InvalidOperationException("Not Style!");
            }

            MainWindow win = (MainWindow)parameter;
            if (value == null)
            {
                return win.Resources["tile_style_default"];
            }
            else
            {
                int v = (int)value;
                switch (v)
                {         
                    case 0: return win.Resources["tile_style_0"];
                    case 1: return win.Resources["tile_style_1"];
                    case 2: return win.Resources["tile_style_2"];
                    case 3: return win.Resources["tile_style_3"];
                    case 4: return win.Resources["tile_style_4"];
                    case 5: return win.Resources["tile_style_5"];
                    case 6: return win.Resources["tile_style_6"];
                    case 7: return win.Resources["tile_style_7"];
                    case 8: return win.Resources["tile_style_8"];
                    case 9: return win.Resources["tile_style_9"];
                    case 10: return win.Resources["tile_style_10"];
                    case 11: return win.Resources["tile_style_11"];
                    case -1: return win.Resources["tile_style_default"];
                    default:
                        return win.Resources["tile_style_default"];
                }
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
